﻿@model Citi.GocAttributes.WebUI.Models.AutoCompleteLevelModel
<script type="text/javascript">
    var functions = [];
    var functionsId = {};

    $(function () {
        $('#@Model.AutoCompleteInput').typeahead({
            source: function (query, process) {
                var url = '@Url.Action("GetAutocomplete", "Function")';

                return $.getJSON(url, { searchTerm: query, level: $("#@Model.LevelInput").val() }, function (data) {
                    functions = [];
                    functionsId = {};

                    $.each(data, function (i, item) {
                        functionsId[item.Description] = item;
                        functions.push(item.Description);
                    });

                    return process(functions);
                });
            }
        }).blur(function () {
            if (functionsId[$(this).val()] == null) {
                $('#@Model.AutoCompleteInput').val('');
                $('#@Model.HiddenIdInput').val('');
            } else {
                $('#@Model.HiddenIdInput').val(functionsId[$(this).val()].Id);
            }
        });
    });
</script>
